package com.zhihuishu.micro.course.openapi.course.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.zhihuishu.micro.course.dao.CcCourseMapper;
import com.zhihuishu.micro.course.model.CcCourse;
import com.zhihuishu.micro.course.openapi.course.CcCourseOpenService;
import com.zhihuishu.micro.course.openapi.course.dto.CcCourseOpenDto;
import com.zhihuishu.micro.course.openapi.online.CourseOpenService;
import com.zhihuishu.remote.ErrorCode;
import com.zhihuishu.remote.RemoteException;
import com.zhihuishu.remote.RemoteResult;
import com.zhihuishu.toolkit.helper.BeanHelper;
import com.zhihuishu.toolkit.jedis.template.JedisExecutor;

@Service("ccCourseOpenService")
public class CcCourseOpenServiceImpl implements CcCourseOpenService {

    @Resource
    private CcCourseMapper ccCourseMapper;

    @Resource
    private CourseOpenService courseOpenService;

    @Resource
    private JedisExecutor jedisExecutor;

    @Override
    public RemoteResult<Void> updateCcCourse(CcCourseOpenDto ccCourseOpenDto) throws RemoteException {
        RemoteResult<Void> result = new RemoteResult<Void>();
        if (null == ccCourseOpenDto) {
            result.setSuccess(false);
            result.setResult(null);
        }

        try {
            CcCourse ccCourse = BeanHelper.transTo(ccCourseOpenDto, CcCourse.class);
            ccCourseMapper.update(ccCourse);
            result.setSuccess(true);
            result.setResult(null);
        } catch (Exception e) {
            throw new RemoteException(e).setMessage("课程发布出错!");
        }
        return result;
    }

    @Override
    public RemoteResult<Map<String, Integer>> findProgressByCourseId(Long courseId) throws RemoteException {
        RemoteResult<Map<String, Integer>> result = new RemoteResult<Map<String, Integer>>();
        if (courseId == null) {
            result.setSuccess(false);
            result.setExceptionStack(new Exception("参数不合法:课程id为空！"));
            result.setErrorCode(ErrorCode.ARGUMENTS_ILLEGALITY);
            return result;
        }
        try {
            Map<String, Integer> map = null;
            List<Long> courseIds = new ArrayList<Long>();
            courseIds.add(courseId);
            RemoteResult<Map<Long, Map<String, Integer>>> progressResult = courseOpenService
                    .findProgressByCourseIds(courseIds);
            Map<Long, Map<String, Integer>> progressMap = progressResult.getResult();
            if (progressMap != null && progressMap.size() > 0) {
                map = progressMap.get(courseId);
            }
            result.setResult(map);
        } catch (Exception e) {
            e.printStackTrace();
            result.setSuccess(false);
            throw new RemoteException(e).setMessage("查询进度出错!");
        }
        return result;
    }

    @Override
    public RemoteResult<Void> delProgressByCourseId(Long courseId) throws RemoteException {
        List<Long> courseIds = new ArrayList<Long>();
        courseIds.add(courseId);
        return courseOpenService.delProgressByCourseIds(courseIds);
    }

}
